package com.manage.model;

import java.io.Serializable;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.manage.db.MenuTypeDBAdapter;

import android.content.Context;
import android.util.Log;

public class Shop implements Serializable {
	
	public static String TAG = "Shop";
	private static final long serialVersionUID = 1L;
	
	private String shopID = ""; // 商户ID
	private String shopName = ""; // 商户名
	private String lastLoginTime = ""; // 最后登录时间
	private String successfulOrders = ""; // 成功订单数
	private String totalEvaluation = ""; // 总评价数
	private String taste = ""; // 口味
	private String speed = ""; // 速度
	private String service = ""; // 服务
	private String turnover = ""; // 营业额
	private String acceptOrder = ""; // 待受理订单
	private String deliverOrder = ""; // 待配送订单
	private String incomeOrder = ""; // 待入账订单
	private String accessToken = ""; // 用户访问令牌
//	private boolean signinReturn = false; // 登录是否成功
	
	// ========get method============
	public String getShopID() {
		return this.shopID;
	}
	
	public String getShopName() {
		return this.shopName;
	}

	public String getLastLoginTime() {
		return this.lastLoginTime == null ? "":this.lastLoginTime;
	}

	public String getSuccessfulOrders() {
		return this.successfulOrders;
	}

	public String getTotalEvaluation() {
		return this.totalEvaluation;
	}

	public String getTaste() {
		return this.taste;
	}

	public String getSpeed() {
		return this.speed;
	}

	public String getService() {
		return this.service;
	}

	public String getTurnover() {
		return this.turnover;
	}

	public String getAcceptOrder() {
		return this.acceptOrder;
	}
	
	public String getDeliverOrder() {
		return this.deliverOrder;
	}
	
	public String getIncomeOrder() {
		return this.incomeOrder;
	}

	public String getAccessToken() {
		return this.accessToken;
	}
	
//	public boolean getSigninReturn() {
//		return this.signinReturn;
//	}
	
	// ==============set method=================
	public void setShopID(String paramString) {
		this.shopID = paramString;
	}

	public void setShopName(String paramString) {
		this.shopName = paramString;
	}

	public void setLastLoginTime(String paramString) {
		this.lastLoginTime = paramString;
	}

	public void setSuccessfulOrders(String paramString) {
		this.successfulOrders = paramString;
	}

	public void setTotalEvaluation(String paramString) {
		this.totalEvaluation = paramString;
	}

	public void setTaste(String paramString) {
		this.taste = paramString;
	}

	public void setSpeed(String paramString) {
		this.speed = paramString;
	}

	public void setService(String paramString) {
		this.service = paramString;
	}
	
	public void setTurnover(String paramString) {
		this.turnover = paramString;
	}
	
	public void setAcceptOrder(String paramString) {
		this.acceptOrder = paramString;
	}
	
	public void setDeliverOrder(String paramString) {
		this.deliverOrder = paramString;
	}
	
	public void setIncomeOrder(String paramString) {
		this.incomeOrder = paramString;
	}

	public void setAccessToken(String paramString) {
		this.accessToken = paramString;
	}
	
//	public void setSigninReturn(boolean paramBoolean) {
//		this.signinReturn = paramBoolean;
//	}
	
	/**
	 * 处理服务器返回的json数据
	 * @param paramJson
	 * @return
	 */
	public static Shop setJson(String paramJson, Context paramContext) {
		Shop shop = new Shop();
		
		if(paramJson != null) {
			try {
				// 临时JSONArray,用来处理服务器返回的数据 
				JSONObject jsonTemp = null;
				JSONObject array = null;
				jsonTemp = new JSONObject(paramJson);
				
				String return_info_success = jsonTemp.get("return_info").toString();
				
				if(return_info_success != null) {
					array = new JSONObject(return_info_success);
					shop.setShopID(array.getString("shop_id").toString());
					shop.setShopName(array.getString("shop_name").toString());
					shop.setLastLoginTime(array.getString("last_login").toString());
					shop.setSuccessfulOrders(array.getString("successful_orders").toString());
					shop.setTotalEvaluation(array.getString("evaluation").toString());
					shop.setTaste(array.getString("taste").toString());
					shop.setSpeed(array.getString("speed").toString());
					shop.setService(array.getString("service").toString());
					shop.setTurnover(array.getString("turnover").toString());
					shop.setAcceptOrder(array.getString("accept_order").toString());
					shop.setDeliverOrder(array.getString("deliver_order").toString());
					shop.setIncomeOrder(array.getString("income_order").toString());
					shop.setAccessToken(array.getString("access_token").toString());
					
					JSONArray arrayTemp = array.getJSONArray("caipin_sort");
					
					MenuTypeDBAdapter mDBAdapter = new MenuTypeDBAdapter(paramContext);
					mDBAdapter.open();
					for(int i=0; i<arrayTemp.length(); i++) {
						JSONObject jsonTemp2 = arrayTemp.getJSONObject(i);
						Log.i(TAG, jsonTemp2.toString());
						mDBAdapter.insertData(jsonTemp2.getString("id"), jsonTemp2.getString("sort"));
					}
					mDBAdapter.close();
				}
				
			} catch (JSONException e) {
				e.printStackTrace();
				shop = null;
			} catch (Exception e) {
				e.printStackTrace();
				shop = null;
			}
			
		}
		return shop;
	}
}
